<?php
declare(strict_types=1);

namespace App\Common\Modular\Administer\Serve;

use App\Common\Modular\Administer\Model\JobModel;
use Core\Basics\BasicsModel;
use Core\Basics\BasicsServe;
use Core\Constants\Database;
use Core\Package\ElementPlus\FormCreate\Form;
use Core\Package\ElementPlus\FormCreate\FormItem;
use Zms\Exception\AppException;
use Zms\Unit\Is;
use Hyperf\Database\Model\Builder;

class JobServe extends BasicsServe
{
    /**
     * @return RoleModel::class
     */
    public static function model(): string
    {
        return JobModel::class;
    }

    /**
     * 重构添加职位
     * @throws AppException
     */
    public function created(array $values): BasicsModel|false
    {
        if ($this->exists(['name' => $values['name']])) {
            throw new AppException("职位'{$values['name']}'已存在");
        }
        return parent::created($values); // TODO: Change the autogenerated stub
    }


    /**
     * 名称搜索器
     * @param Builder $query
     * @param $keyword
     * @return void
     */
    public function searchName(Builder $query, $keyword): void
    {
        if (Is::notEmpty($keyword)) {
            if (is_string($keyword)) {
                $query->where('name', 'LIKE', "%$keyword%");
            }
        }
    }


    /**
     * 添加职位表单
     * @return Form
     */
    public function createdForm(): Form
    {
        $rule[] = FormItem::Input('name', '职位')->clearable(true)->maxlength(10)->required('请输入分类名称');
        $rule[] = FormItem::Textarea('remarks', '备注')->clearable(true)->maxlength(30);
        $rule[] = FormItem::Number('sort', '排序')->placeholder('序号越大越靠前')->min(Database::INT_MIN)->max(Database::INT_MAX)->clearable(true)->col(12);
        $rule[] = FormItem::Switchs('status', '状态', '1')->col(12);
        return Form::create('添加职位')->setRules($rule)->setOptionFormInline(true);
    }


    /**
     * 编辑职位表单
     * @param mixed $id
     * @return Form
     * @throws AppException
     */
    public function updatedForm(mixed $id = ''): Form
    {
        /**
         * @var JobModel $model
         */
        $model = $this->search(['id' => $id ?: 0])->first();
        if (!$model) {
            throw new AppException('职位已删除');
        }
        $rule[] = FormItem::Hidden('id', $model->id);
        $rule[] = FormItem::Input('name', '职位', $model->name)->clearable(true)->maxlength(10)->required('请输入分类名称');
        $rule[] = FormItem::Textarea('remarks', '备注', $model->remarks)->clearable(true)->maxlength(30);
        $rule[] = FormItem::Number('sort', '排序', $model->sort)->placeholder('序号越大越靠前')->min(Database::INT_MIN)->max(Database::INT_MAX)->clearable(true)->col(12);
        $rule[] = FormItem::Switchs('status', '状态', (string)$model->status)->col(12);
        return Form::create('编辑职位')->setRules($rule)->setOptionFormInline(true);
    }


    /**
     * 获取职位数组
     * @return array
     */
    public function JobFormData(): array
    {
        $data = $this->search(['status' => 1])->select(['id as value', 'name as label'])->orderByRaw('sort DESC,id DESC')->get()->toArray();
        foreach ($data as &$item){
            $item['value']=(string)$item['value'];
        }
        return $data;
    }
}